//
//  LTFTopicContentViewController.m
//  NeteaseNews
//
//  Created by liutaifeng on 16/4/20.
//  Copyright © 2016年 liutaifeng. All rights reserved.
//

#import "LTFTopicContentViewController.h"
#import "LTFTopicContentView.h"
#import "LTFTopicBaseHandel.h"
#import "IsNight.h"
@interface LTFTopicContentViewController ()
<
 LTFTopicContentViewDelegate
>
@property (nonatomic, retain) LTFTopicContentView *topicContentView;
@property (nonatomic, retain) UIView *myView;

@property (nonatomic, retain) UIButton *rightButton;

@property (nonatomic, retain) UIButton *leftButton;

@property (nonatomic, assign) BOOL isCollect;
@end

@implementation LTFTopicContentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createView];
    
}

- (void)createView {
    
    self.isCollect = NO;
    
    
    //self.navigationController.navigationBarHidden = YES;
    self.topicContentView = [[LTFTopicContentView alloc] initWithFrame:self.view.frame];
    [self.view addSubview:self.topicContentView];
    [_topicContentView release];
    self.topicContentView.string = self.string;
    self.topicContentView.delegate = self;
    NSMutableArray *mArray =  [[LTFTopicBaseHandel shareDataBaseTool] select];
    for (NSInteger i = 0; i < mArray.count; i++) {
        if ([mArray[i] isEqualToString:self.string]) {
            self.isCollect = YES;
            break;
        }
        else {
            self.isCollect = NO;
        }
    }
    
    
    self.myView = [[[UIView alloc] initWithFrame:CGRectMake(0, 24, self.view.frame.size.width, 40)] autorelease];
    
    self.myView.backgroundColor = [UIColor whiteColor];
    [self.navigationController.view addSubview:_myView];
    
    self.leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_leftButton setImage:[UIImage imageNamed:@"fanhui.png"] forState:UIControlStateNormal];
    _leftButton.frame = CGRectMake(0, 0, 40, 40);
    [self.myView addSubview:_leftButton];
    [_leftButton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    
    self.rightButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.myView addSubview:_rightButton];
    _rightButton.frame = CGRectMake(self.view.frame.size.width - 40, 0, 40, 40);
    [_rightButton addTarget:self action:@selector(collect:) forControlEvents:UIControlEventTouchUpInside];
    
    if (self.isCollect == YES) {
        [_rightButton setImage:[UIImage imageNamed:@"xin.png"] forState:UIControlStateNormal];
        _rightButton.tintColor = [UIColor redColor];
    }
    else {
        [_rightButton setImage:[UIImage imageNamed:@"xin1.png"] forState:UIControlStateNormal];
        _rightButton.tintColor = [UIColor redColor];
    }
}

- (void)collect:(UIButton *)button {
    [[LTFTopicBaseHandel shareDataBaseTool] insertModel:self.string];
    
    if (self.isCollect == YES) {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"不可重复收藏" message:nil delegate:nil cancelButtonTitle:@"确认" otherButtonTitles:nil];
            alertView.alertViewStyle = UIAlertViewStyleDefault;
            [alertView show];
    }
    if (self.isCollect == NO) {
        self.isCollect = YES;
    }

    if (self.isCollect == YES) {
        [_rightButton setImage:[UIImage imageNamed:@"xin.png"] forState:UIControlStateNormal];
        _rightButton.tintColor = [UIColor redColor];
    }
}
- (void)viewWillDisappear:(BOOL)animated {
    [_myView removeFromSuperview];
    [_leftButton removeFromSuperview];
    [_rightButton removeFromSuperview];
    
}
- (void)viewWillAppear:(BOOL)animated {
    UITableView *tableView = [self.view viewWithTag:1001];
    [tableView reloadData];
}
- (void)appear:(BOOL)isAppear {
    if (isAppear == YES) {
        [UIView animateWithDuration:0.5 animations:^{
            self.navigationController.navigationBar.translucent = NO;
            _myView.backgroundColor = [UIColor blueColor];
        }];
        
    }
    else {
        [UIView animateWithDuration:0.5 animations:^{
            _myView.backgroundColor = [UIColor whiteColor];
        }];
    }
    
}
- (void)click:(UIButton *)button {
    [self.myView removeFromSuperview];
    [self.navigationController popViewControllerAnimated:YES];
    self.navigationController.navigationBarHidden = NO;
    self.tabBarController.hidesBottomBarWhenPushed = NO;
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
